import { treeToArray } from '@/utils/treeFlattern'
import _lodash from 'lodash'
/**
 * hasPermission 判断是否有路由权限 routes >= roleRoutes
 * @param {*} roleRoutes 角色路由列表 iconName,id,keepAlive,pId,path,title
 * @param {*} routes 文件生成得所有路由列表 path,name,component
 * @returns
 */
function hasPermission(roleRoutes, routes) {
    const resRoutes = []
    const cloneRoleRoutes = _lodash.cloneDeep(roleRoutes)
    const cloneRoutes = _lodash.cloneDeep(routes) // 角色原始权限数据
    // 将角色权限列表转为一维数组,并获取所有的路径组成新数组
    const allList = treeToArray(cloneRoleRoutes)
    allList.forEach((item) => {
        cloneRoutes.forEach((subItem) => {
            if (item.path == subItem.path) {
                const newItem = {
                    ...subItem,
                    ...item
                }
                resRoutes.push(newItem)
            }
        })
    })
    return resRoutes
}

/**
 * filterAsyncRoutes 筛选路由
 * @param {*} roleRoutes 角色路由列表
 * @param {*} routes 文件生成得所有路由列表
 * @returns
 */
export function filterAsyncRoutes(roleRoutes, routes) {
    // 筛选路由
    const routesRes = hasPermission(roleRoutes, routes)
    // 返回角色权限路由
    return routesRes
}
